package initialize

import (
	"fmt"
	"github.com/google/uuid"
	"github.com/hashicorp/consul/api"
	"keep/common/config"
)

func ConSul() {
	nas := config.Nas.Consul
	client, _ := api.NewClient(&api.Config{
		Address: fmt.Sprintf("%s:%d", nas.Host, nas.Port),
	})
	id := uuid.New().String()
	err = client.Agent().ServiceRegister(&api.AgentServiceRegistration{
		ID:      id,
		Name:    nas.Name,
		Tags:    []string{"GRPC"},
		Port:    nas.Port,
		Address: nas.Host,
		Check: &api.AgentServiceCheck{
			Interval:                       "5s",
			Timeout:                        "5s",
			GRPC:                           "30s",
			DeregisterCriticalServiceAfter: "5s",
		},
	})
	if err != nil {
		return
	}
	fmt.Println("consul初始化成功")
}
